﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;

public class ReImportSprite2d  {

	[MenuItem ("Tools/Sprite/reimport-deep")]
	public static void ReImportSprite2dFullRect () {
		UnityEngine.Object[] obs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
		foreach (UnityEngine.Object e in obs) {
			Debug.Log(e.name);

			string p = AssetDatabase.GetAssetPath (e);
			if (".png" == Path.GetExtension(p) ||
				".jpg" == Path.GetExtension(p)) {
				ReImportPNGToSprite(p);
			}
		}
	}

	[MenuItem ("Tools/Sprite/")]
	public static void ReImportSprite2dFullRectBake () {
	}
	//static void getSpriteListFromTextureDic(string path)
	//{
	//	//Dictionary<string, Sprite> mlistSprite = new Dictionary<string, Sprite>();
	//	string[] files = Directory.GetFiles(path); 
	//	foreach (string p in files) {
	//		if (".png" == Path.GetExtension(p) ||
	//			".jpg" == Path.GetExtension(p)) {
	//			ReImportPNGToSprite(p);
	//			//UnityEngine.Object[] ts = AssetDatabase.LoadAllAssetsAtPath(p);
	//			//foreach (UnityEngine.Object t in ts) {
	//			//	if (t as Sprite) {
	//			//		mlistSprite.Add(t.name, t as Sprite);
	//			//	}
	//			//}
	//		}
	//	}
	//	return ;
	//}

	static void ReImportPNGToSprite (string path) {
		TextureImporter texImporter = GetTextureSettings(path);
		texImporter.mipmapEnabled = false;
		TextureImporterSettings tis = new TextureImporterSettings();
		texImporter.ReadTextureSettings(tis);
		tis.spriteMeshType = SpriteMeshType.FullRect;
		tis.spriteAlignment = 1;
		//texImporter.ClearPlatformTextureSettings ("iPhone");
		texImporter.SetTextureSettings(tis);
		AssetDatabase.ImportAsset(path);
	}

	static TextureImporter GetTextureSettings(string path)
	{
		TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
		textureImporter.textureType = TextureImporterType.Sprite;
		//textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
		return textureImporter;
	}
}
